package com.hbnu.iostream;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/**
 * @author kittychen
 * @date 2025-09-30 17:40
 */
public class PipedInputStreamDemo implements Runnable {

    private PipedInputStream pipedInputStream;

    public PipedInputStreamDemo() {
        this.pipedInputStream = new PipedInputStream();
    }

    public PipedInputStream getPipedInputStream() {
        return pipedInputStream;
    }

    @Override
    public void run() {
        byte[] buf = new byte[1024];
        int len = 0;
        int temp;
        try {
            while ((temp = pipedInputStream.read()) != -1) {
                buf[len++] = (byte) temp;
            }

            System.out.println(new String(buf, 0, len));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
